//
//  MTCCraeteClienterViewController.m
//  ERP
//
//  Created by QA on 15/9/17.
//  Copyright (c) 2015年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MTCCraeteClienterViewController.h"
#import "GEInputTableViewCell.h"
#import "WebServiceCommonRequest.h"

#import "GEPickerTextField.h"
#import "NSDictionary+DataFormat.h"
#import "NSArray+Format.h"

@interface MTCCraeteClienterViewController ()

@property (nonatomic, strong) NSMutableDictionary *clienterInfo;

@end

@implementation MTCCraeteClienterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTitle:@"新增客户"];
        UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"提交" style:UIBarButtonItemStyleDone target:self
                                                                      action:@selector(submitClienterSelector:)];
        self.navigationItem.rightBarButtonItem = leftButton;
    }
    return self;
}

- (void)getPassingParameter:(id)object
{
    if (!object) {
        [self setTitle: @"新增客户"];
    } else {
        [self setTitle: @"修改客户信息"];
        NSDictionary *dic = object;
        self.clienterInfo = [NSMutableDictionary dictionaryWithDictionary:dic];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SuperWidth, 10)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIAlertView

- (void)showAlertView:(NSString*)message delegate:(id)delegate
{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"温馨提示" message:message delegate:delegate cancelButtonTitle:@"确定" otherButtonTitles:nil];
    alert.alertViewStyle=UIAlertViewStyleDefault;
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark Button Selector

- (void)submitClienterSelector:(id)sender
{
    UITextField *textField00 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:0];
    UITextField *textField11 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    if (textField00.text.length <= 0) {
        [self showAlertView:@"请输入客户名称" delegate:nil];
    } else if (textField11.text.length <= 0) {
        [self showAlertView:@"请选择注册时间" delegate:nil];
    } else {
        [self submitNewClienter];
    }
}

#pragma mark 网络提交数据

- (void)submitNewClienter
{
    UITextField *textField00 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:0];
    UITextField *textField02 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:0];
    UITextField *textField03 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:0];
    UITextField *textField04 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:4 inSection:0];
    UITextField *textField05 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:1];
    UITextField *textField06 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:1];
    UITextField *textField07 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:1];
    UITextField *textField08 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:2];
    UITextField *textField09 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:2];
    UITextField *textField10 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:2];
    UITextField *textField11 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:2];
    UITextField *textField12 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:0 inSection:3];
    UITextField *textField13 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:3];
    UITextField *textField14 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:2 inSection:3];
    UITextField *textField15 = [self.tableView getCellSaveClass:[UITextField class] pathForRow:3 inSection:3];
    
    //评分处理
    GEPickerTextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
    NSNumber *xjValue = [textField.selectData objectFormatIntegerNumberForKey:@"value"];
    
    //客户编号处理
    NSString *clienterNumber = @"";
    if (self.clienterInfo) {
        clienterNumber = [self.clienterInfo objectFormatStringForKey:@"客户编号"];
    }
    
    //注册资金
    NSNumber *zczj = [NSNumber numberWithInteger:[textField05.text integerValue]];
    
    WebServiceCommonRequest *content = [[WebServiceCommonRequest alloc] init];
    [content setShowLoadingPrompt:self.navigationController labelText:@"提交客户信息中"];
    [content setInterfaceType:InterfaceTypeGroup];
    [content setModuleType:@"KHDA_KHLB_AU"];
    [content setResultBackType:ResultBackTypeCommonUnordered];
    
    [content addParameterKey:@"客户编号" value:clienterNumber];
    [content addParameterKey:@"客户名称" value:textField00.text];
    [content addParameterKey:@"客户星级" value:xjValue];
    [content addParameterKey:@"客户法人" value:textField02.text];
    [content addParameterKey:@"电话" value:textField03.text];
    [content addParameterKey:@"传真" value:textField04.text];
    [content addParameterKey:@"注册资金" value:zczj];
    [content addParameterKey:@"注册日期" value:textField06.text];
    [content addParameterKey:@"注册地" value:textField07.text];
    [content addParameterKey:@"开户银行" value:textField08.text];
    [content addParameterKey:@"公司账户" value:textField09.text];
    [content addParameterKey:@"税号" value:textField10.text];
    [content addParameterKey:@"财务联系人" value:textField11.text];
    [content addParameterKey:@"办公地址" value:textField12.text];
    [content addParameterKey:@"经营范围" value:textField13.text];
    [content addParameterKey:@"快递地址" value:textField14.text];
    [content addParameterKey:@"备注" value:textField15.text];
    
    [content startAsyncConnectCompleted:^(ServiceResultModel *result, NSDictionary *userInfo) {
        NSArray *firstArray = nil;
        if(result.resultArray && result.resultArray.count>0)
            firstArray = [result.resultArray firstObject];
        
        if(firstArray && firstArray.count>0){
            [self showAlertView:@"提交成功" delegate:self];
            NSDictionary *valueDic = [firstArray firstObject];
            if(self.backResultDelagate) [self.backResultDelagate backResultDataKey:self.key dataValue:valueDic];
            
        } else {
            [self showAlertView:@"提交客户信息失败，请检查填写内容是否正确" delegate:nil];
        }
    } failed:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showAlertView:@"系统繁忙，请稍后再试" delegate:nil];
        
    } abnormal:^(NSError *error, NSDictionary *userInfo) {
        NSLog(@"=====%@",error);
        [self showAlertView:@"网络连接失败，请检查网络是否正常" delegate:nil];
    }];
    
}

#pragma mark
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0) return 5;
    if(section == 1) return 3;
    if(section == 2) return 4;
    if(section == 3) return 4;
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = [NSString stringWithFormat:@"MTCCraeteClienterViewController%ld%ld",indexPath.section,indexPath.row];
    
    //合同基本信息
    if(indexPath.section == 0){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"客户名称";
            cell.placeholder = @"请输入客户名称";
            cell.isMark = YES;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"客户名称"];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithSelectReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"客户星级";
            cell.placeholder = @"请选择客户星级";
            cell.isMark = NO;
            cell.isEdit = NO;
            [cell setEnablePrevious:NO next:NO];
            [cell setKeyboardType:UIKeyboardTypePhonePad filter:nil];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            
            NSArray *resultArray = @[@{@"Definition":@"无评级",@"value":@0},@{@"Definition":@"一星级",@"value":@1},
                                     @{@"Definition":@"二星级",@"value":@2},@{@"Definition":@"三星级",@"value":@3},
                                     @{@"Definition":@"四星级",@"value":@4},@{@"Definition":@"五星级",@"value":@5}];
            NSDictionary *QDDJ = [NSDictionary dictionaryWithObject:@"%@" forKey:@"Definition"];
            [cell setPickerTextFieldComponents:resultArray keyValue:@[QDDJ]];
            
            GEPickerTextField *textF = [self.tableView getCellSaveClass:[UITextField class] pathForRow:1 inSection:0];
            if (self.clienterInfo) { //修改客户信息处理
                NSInteger xjValue = [self.clienterInfo objectFormatIntegerForKey:@"客户星级"];
                NSDictionary *resultItem = [resultArray objectAtIndex:xjValue];
                if (resultItem) {
                    textF.selectData = resultItem;
                    textF.text = [resultItem objectFormatStringForKey:@"Definition"];
                }
            }
            if(!textF.selectData) textF.selectData = @{@"Definition":@"无评级",@"value":@0};
            textF.text = @"无评级";
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"客户法人";
            cell.placeholder = @"请输入法人";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"客户法人"];
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"联系电话";
            cell.placeholder = @"请输入联系电话";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789+"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"电话"];
            
        } else if(indexPath.row == 4){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"传      真";
            cell.placeholder = @"请输入传真";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789+"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"传真"];
            
        }
        return cell;
    }
    
    //信息2
    else if(indexPath.section == 1){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        
        if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"注册资金";
            cell.placeholder = @"请输入注册资金";
            cell.isMark = NO;
            cell.isEdit = YES;
            [cell setKeyboardType:UIKeyboardTypeDecimalPad filter:@"0123456789"];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"注册资金"];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithDateReuseIdentifier:CellIdentifier];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.titleName = @"注册日期";
            cell.placeholder = @"请选择注册日期";
            cell.isMark = NO;
            [cell setDatePickerMode:UIDatePickerModeDate];
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"注册日期"];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"注  册  地";
            cell.placeholder = @"请输入注册地";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"注册地"];
            
        }
        return cell;
    }
    
    //信息3
    else if(indexPath.section == 2){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        else if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"开户银行";
            cell.placeholder = @"请输入开户银行";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"开户银行"];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"公司账户";
            cell.placeholder = @"请输入公司账户";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"公司账户"];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"公司税号";
            cell.placeholder = @"请输入公司税号";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"税号"];
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"财务联系人";
            cell.placeholder = @"请输入财务联系人";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"财务联系人"];
            
        }
    
        return cell;
    }
    
    //信息4
    else if(indexPath.section == 3){
        GEInputTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell != nil) return cell;
        else if(indexPath.row == 0){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"办公地址";
            cell.placeholder = @"请输入办公地址";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"办公地址"];
            
        } else if(indexPath.row == 1){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"经营范围";
            cell.placeholder = @"请输入经营范围";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"经营范围"];
            
        } else if(indexPath.row == 2){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"快递地址";
            cell.placeholder = @"请输入快递地址";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"快递地址"];
            
        } else if(indexPath.row == 3){
            cell = [[GEInputTableViewCell alloc] initWithReuseIdentifier:CellIdentifier model:nil];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.titleName = @"备      注";
            cell.placeholder = @"可不填";
            cell.isMark = NO;
            cell.isEdit = YES;
            [self.tableView setCellView:cell inRow:indexPath.row inSection:indexPath.section];
            if (self.clienterInfo) cell.inputFiled = [self.clienterInfo objectFormatStringForKey:@"备注"];
        }
        
        return cell;
    }
    
    return nil;
}

#pragma mark
#pragma mark - UITableViewDelegate

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //键盘事件处理
    UITextField *textField = [self.tableView getCellSaveClass:[UITextField class] pathForRow:indexPath.row inSection:indexPath.section];
    if (textField) [textField becomeFirstResponder];
    
}

@end
